package co.jp.roomy;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import co.jp.roomy.vo.Heya;
import co.jp.roomy.vo.MyKagu;

public class MyKaguListActivity extends Activity {

    private Heya heya;
    private DatabaseHelper helper = new DatabaseHelper(this);
    public static SQLiteDatabase db;
    private String[] kaguList;
    
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.my_kagu_list);
        
        // もうちょいもごもごする
        db = helper.getReadableDatabase();
        String[] columns = {"myKaguId","myKaguName"};
        String[] selections = new String[1];
        selections[0] = heya.getHeyaId().toString();
        String orderBy = "myKaguId ASC";
        Cursor c = db.query("T_MYKAGU", columns, "heyaId = ?", selections, null, null, orderBy);
        startManagingCursor(c);
        kaguList = new String[c.getCount()];
        Intent intent = getIntent();
        heya = (Heya) intent.getSerializableExtra(MyKaguListActivity.this.getString(R.string.heyaKey));
        
        final ListView listView = (ListView) findViewById(R.id.list);
        List<MyKagu> myKaguList = heya.getMykaguList();
		for (int i=0; i<c.getCount();i++) {
			c.moveToNext();
			kaguList[i] = c.getString(c.getColumnIndex("myKaguName"));
		}
		c.close();
        List<String> list = new ArrayList<String>();
        
        for (MyKagu myKagu : myKaguList) {
            list.add(myKagu.getMyKaguName());
        }

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this, 
                android.R.layout.simple_list_item_1,
                list);

        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, 
                                    View view,
                                    int position, 
                                    long id) {
                ListView listView = (ListView) parent;

                Intent intent = new Intent(getApplicationContext(), MyKaguCustomActivity.class);
                
                final String item = (String) listView.getItemAtPosition(position);
                List<MyKagu> myKaguList = heya.getMykaguList();
                MyKagu myKaguCustom = null;
                for (MyKagu myKagu : myKaguList) {
                    if (myKagu.getMyKaguName().equals(item)) {
                    	myKaguCustom = myKagu;
                    }
                }
                
                //TODO消す
                myKaguCustom.setMyKaguSizeX1(0);
                myKaguCustom.setMyKaguSizeX2(1);
                myKaguCustom.setMyKaguSizeX3(1);
                myKaguCustom.setMyKaguSizeX4(0);
                myKaguCustom.setMyKaguSizeY1(0);
                myKaguCustom.setMyKaguSizeY2(1);
                myKaguCustom.setMyKaguSizeY3(1);
                myKaguCustom.setMyKaguSizeY4(0);
                myKaguCustom.setMyKaguSizeZ1(0);
                myKaguCustom.setMyKaguSizeZ2(1);
                myKaguCustom.setMyKaguSizeZ3(1);
                myKaguCustom.setMyKaguSizeZ4(0);
                myKaguCustom.setMyKaguMemo("memomemo");
                
                intent.putExtra(MyKaguListActivity.this.getString(R.string.myKaguCustomKey), myKaguCustom);
                
                intent.putExtra(MyKaguListActivity.this.getString(R.string.heyaKey), heya);
                startActivity(intent);
            }
        });
    }

	/**
	 * 戻るボタン押下時
	 * @param View view
	 */
	public void madoriButtonOnClick(View view) {
		Intent intent = new Intent(MyKaguListActivity.this, MadoriActivity.class);
		intent.putExtra(MyKaguListActivity.this.getString(R.string.heyaKey), heya);
		startActivity(intent);
	}

	/**
	 * 家具追加ボタン押下時
	 * @param View view
	 */
	public void selectKaguCategoryButtonOnClick(View view) {
		Intent intent = new Intent(MyKaguListActivity.this, MyKaguCustomActivity.class);
		MyKagu myKaguCustom = new MyKagu();
        intent.putExtra(getApplicationContext().getString(R.string.myKaguCustomKey), myKaguCustom);
		intent.putExtra(MyKaguListActivity.this.getString(R.string.heyaKey), heya);
		startActivity(intent);
	}
}
